import {
  delSourceByIds,
  getSourceDetail,
  getSourceList,
  getSourceOptionList,
  sourceSave,
  updateDefaultSourceById,
  updateSourceStateByIds,
} from '@/api/order'
import { ref } from 'vue'

/**
 * 获取工单来源列表
 */
export const useGetOrderSourceList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref({
    limit: 50,
    page: 1,
    name: '',
  })

  // 列表总条数
  const listTotal = ref(0)

  // 列表
  const list = ref<Array<OrderSource>>([])

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await getSourceList(getListParam.value)
    list.value = res.data.data.rows
    listTotal.value = Number(res.data.data.total)
    getListLoading.value = false
  }

  return {
    list,
    listTotal,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 批量启用来源
 */
export const useEnableSourceByIds = () => {
  // 启用加载
  const enableLoading = ref(false)

  /**
   * 启用
   * @param ids - 来源id集合
   */
  const enable = async (ids: Array<string>) => {
    enableLoading.value = true
    const res = await updateSourceStateByIds(ids, false)
    enableLoading.value = false
    return res.data
  }

  return {
    enableLoading,
    enable,
  }
}

/**
 * 批量禁用来源
 */
export const useDisableSourceByIds = () => {
  // 禁用加载
  const disableLoading = ref(false)

  /**
   * 禁用
   * @param ids - 来源id集合
   */
  const disable = async (ids: Array<string>) => {
    disableLoading.value = true
    const res = await updateSourceStateByIds(ids, true)
    disableLoading.value = false
    return res.data
  }

  return {
    disableLoading,
    disable,
  }
}

/**
 * 批量删除来源
 */
export const useDelSourceByIds = () => {
  // 删除加载
  const delLoading = ref(false)

  /**
   * 删除
   * @param ids - 来源id集合
   */
  const del = async (ids: Array<string>) => {
    delLoading.value = true
    const res = await delSourceByIds(ids)
    delLoading.value = false
    return res.data
  }

  return {
    delLoading,
    del,
  }
}

/**
 * 更新默认来源
 */
export const useUpdateDefaultSource = () => {
  // 更新加载
  const updateLoading = ref(false)

  /**
   * 更新
   * @param id - 来源id
   */
  const update = async (id: string) => {
    updateLoading.value = true
    const res = await updateDefaultSourceById(id)
    updateLoading.value = false
    return res.data
  }

  return {
    updateLoading,
    update,
  }
}

/**
 * 来源创建
 */
export const useSourceForm = () => {
  // 创建加载
  const saveLoading = ref(false)

  // 表单
  const form = ref<OrderAPIRequest.SourceForm>({
    code: '',
    defaultSelect: false,
    id: '',
    status: false,
    name: '',
  })

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      code: '',
      defaultSelect: false,
      id: '',
      status: false,
      name: '',
    }
  }

  /**
   * 保存
   */
  const save = async () => {
    saveLoading.value = true
    const res = await sourceSave(form.value)
    saveLoading.value = false
    return res.data
  }

  return {
    form,
    saveLoading,
    save,
    resetForm,
  }
}

/**
 * 获取来源详情
 */
export const useGetSourceDetail = () => {
  // 获取详情加载
  const getDetailLoading = ref(false)

  // 详情
  const sourceDetail = ref<OrderSourceDetail>({
    defaultSelect: false,
    status: false,
    name: '',
    id: '',
    code: '',
  })

  /**
   * 获取详情
   * @param id - 来源id
   */
  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await getSourceDetail(id)
    sourceDetail.value = res.data.data.row
    getDetailLoading.value = false
  }

  return {
    sourceDetail,
    getDetailLoading,
    getDetail,
  }
}

/**
 * 获取来源下拉选项
 */
export const useGetSourceOptionList = () => {
  // 获取下拉选项加载
  const getOptionListLoading = ref(false)

  // 获取下拉选项请求参数
  const getOptionListParam = ref({
    name: '',
    page: 1,
    limit: 50,
  })

  // 下拉选项列表
  const optionList = ref<Array<OrderSourceOption>>([])

  /**
   * 获取下拉列表
   */
  const getOptionList = async () => {
    getOptionListLoading.value = true
    const res = await getSourceOptionList(getOptionListParam.value)
    optionList.value = res.data.data.rows
    getOptionListLoading.value = false
  }

  return {
    optionList,
    getOptionListParam,
    getOptionListLoading,
    getOptionList,
  }
}
